/****************************************
Header name: Manip.c
Author: Gu Zhengxiong
Version: 2013.11.21
Description:The file implements functions in MANIP.
****************************************/

# include <stdlib.h>
# include <stdio.h>
# include <string.h>

# include "StudentInfo.h"
# include "StudentGrade.h"
# include "Student.h"
# include "Manip.h"

PSTUDENT
Construct (void)
{
	PSTUDENT ps = (PSTUDENT)malloc (sizeof (STUDENT));
	ps->pnext = NULL;
	return ps;
}
void
Add (PSTUDENT phead, PINFO pi_in, PGRADE pg_in)
{
	PSTUDENT ps = (PSTUDENT)malloc (sizeof (STUDENT));
	PSTUDENT ps_tmp = phead;
	ps->pi = pi_in;
	ps->pg = pg_in;
	ps->Display = Display;
	while (ps_tmp->pnext != NULL)
		ps_tmp = ps_tmp->pnext;
	ps_tmp->pnext = ps;
	ps->pnext = NULL;
}
void
DisplayInfo (PSTUDENT phead)
{
	PSTUDENT ps_tmp = phead;
	while (ps_tmp != NULL)
	{
		if (ps_tmp == phead)
			;
		else
		{
			printf (ALL_INFO_FORMAT, ALL_INFO_PARA);
		}
			ps_tmp = ps_tmp->pnext;
	}
}
void
DisplayGrade (PSTUDENT phead)
{
	PSTUDENT ps_tmp = phead;
	while (ps_tmp != NULL)
	{
		if (ps_tmp == phead)
			;
		else
		{
			printf (ALL_GRADE_FORMAT, ALL_GRADE_PARA);
		}
			ps_tmp = ps_tmp->pnext;
	}
}
PSTUDENT
Inquire (PSTUDENT phead, int type, char * s)
{
	PSTUDENT ps_tmp = phead;
	switch (type)
	{
		case SEARCH_NUM:
			{
				int flag = 0;
				while (ps_tmp != NULL)
				{
					if (strcmp (s, ps_tmp->pi->number) == 1)
					{
						flag = 1;
						return ps_tmp;
					}
					else
						ps_tmp = ps_tmp->pnext;
				}
				if (flag == 0)
					return 0;
			}; break;
		case SEARCH_NAME:
			{
				int flag = 0;
				while (ps_tmp != NULL)
				{
					if (strcmp (s, ps_tmp->pi->name) == 1)
					{
						flag = 1;
						return ps_tmp;
					}
					else
						ps_tmp = ps_tmp->pnext;
				}
				if (flag == 0)
					return 0;
			}; break;
	}
}
void
Destroy (PSTUDENT phead)
{
	PSTUDENT ps_tmp = phead;
	while (ps_tmp != NULL)
	{
		if (ps_tmp == phead)
			;
		else
		{	free (ps_tmp->pg);
			free (ps_tmp->pi);
		}
		ps_tmp = ps_tmp->pnext;
	}
}
PMANIP
ManipInitialize (void)
{
	PMANIP pm = (PMANIP)malloc (sizeof (MANIP));
	pm->Construct = Construct;
	pm->Add = Add;
	pm->DisplayInfo = DisplayInfo;
	pm->DisplayGrade = DisplayGrade;
	pm->Destroy = Destroy;

	return pm;
}